package com.zjw.socket.demo.starter.config;

import com.zjw.socket.demo.starter.utils.LogUtils;
import com.zjw.socket.demo.starter.core.Reflector;
import io.reactivex.rxjava3.annotations.NonNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * 参考 application.properties
 */
public final class ApplicationConfig {

  private static final String TAG = "ApplicationConfig";


  /**
   * 一个可拓展功能的目录，
   *
   * @see com.zjw.socket.demo.starter.plugin.IPlugin
   * @see com.zjw.socket.demo.starter.annotation.core.Plugin
   * @see com.zjw.socket.demo.starter.plugin.PluginDispatcher
   */

  private static final Properties configProperties = new Properties();

  public static void initConfig() {
    LogUtils.debug(TAG, "start config");

    InputStream resourceAsStream = Reflector.sApplication.getClassLoader().getResourceAsStream("application.properties");
    try {
      configProperties.load(resourceAsStream);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    LogUtils.debug(TAG, "end config");
  }

  public static String[] getPlugPathList() {
    String pathList = configProperties.getProperty("plug.path.list");
    if (pathList != null) {
      return pathList.split(",");
    }
    return new String[0];
  }

  @NonNull
  public static String getString(String key) {
    return configProperties.getProperty(key);
  }

  @NonNull
  public static int getInt(String key) {
    return Integer.parseInt(configProperties.getProperty(key));
  }

  @NonNull
  public static boolean getBoolean(String key) {
    return Boolean.parseBoolean(configProperties.getProperty(key));
  }
}
